package com.pda.xinma.presenter;

import com.pda.xinma.bean.BackDepotListBean;
import com.pda.xinma.bean.ScanResultBean;
import com.pda.xinma.bean.WholePalletInfoBean;
import com.pda.xinma.bean.request.InventoryConfirmRequst;
import com.pda.xinma.contract.CheckingContract;
import com.pda.xinma.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.net.bean.BaseRowsAppEntity;
import com.wangshen.base.net.bean.ImageEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.io.File;
import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class CheckingPresenter extends BasePresenter<CheckingContract .View> implements  CheckingContract.Presenter{
    @Override
    public void wholePalletInventoryConfirm(InventoryConfirmRequst body) {
        NetUtils.geApi().wholePalletInventoryConfirm(body)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> scanResultBeanBaseAppEntity) throws Exception {
                            getView().showMessage(scanResultBeanBaseAppEntity.getMsg());
                            getView().onSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void upload(String path) {
        File file=new File(path);
        RequestBody body=RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), body);
        NetUtils.geApi().upload(part)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<ImageEntity>() {
                    @Override
                    public void accept(ImageEntity listBaseAppEntity) throws Exception {
                        getView().setImageEntity(listBaseAppEntity);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void countBackConfirm(String storageBinId) {
        NetUtils.geApi().countBackConfirm(storageBinId)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<BackDepotListBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<BackDepotListBean>> listBaseRowsAppEntity) throws Exception {
                        getView().showMessage(listBaseRowsAppEntity.getMsg());
                        getView().onBackSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getWholePalletInfo(String taskNo, String container, String measure, String rollerId) {
        NetUtils.geApi().getWholePalletInfo(taskNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<WholePalletInfoBean>>() {
                    @Override
                    public void accept(BaseAppEntity<WholePalletInfoBean> wholePalletInfoBeanBaseAppEntity) throws Exception {
                        getView().setData(wholePalletInfoBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
